<?php
/**
 * @author              : Leif H. Larsen
 * @creationdate        : 24/10/2011
 * @lastchanged         : 25/10/2011
 * @name                : hk_Login
 * @description         : 
 */

/**
 * TODO: Add check to see if user is logged in. Should match the check in CheckLogin.php
 * 
 */
?>

<?php

class hk_Login {
    public function isLoggedIn() {
        if(isset($_SESSION['hkUser']) && isset($_SESSION['hkPass'])):
            return true;
        elseif(isset($_COOKIE['hkUser']) && isset($_COOKIE['hkPass'])):
            if($this->confirmUser($_COOKIE['hkUser'] && $_COOKIE['hkPass'])):
                $this->createSession($_COOKIE['hkUser'], $_COOKIE['hkPass']);
                return true;
            else:
                $this->logout();
                return false;
            endif;
        else:
            return false;
        endif;
    } // End isLoggedIn
    
    public function logout() {
        unset($_SESSION['hkUser']);
        unset($_SESSION['hkPass']);
    
        session_unset();    
        session_destroy(); 

        setcookie ("hkUser", "",time()-60*60*24*100, "/");
        setcookie ("hkPass", "",time()-60*60*24*100, "/"); 
        
        header('location: ./');
    } // End logout
    
    private function createSession($user, $pw) {
        session_register();

        $_SESSION["hkUser"] = $username;
        $_SESSION["hkPass"] = sha1($password);
    
        if(isset($_POST['remme'])) :
            setcookie("hkUser", $_SESSION['hkUser'], time()+60*60*24*100, "/");
            setcookie("hkPass", $_SESSION['hkPass'], time()+60*60*24*100, "/");
            return;
        endif;
    } // End createSession
    
    private function confirmUser($user, $pw) {
        // Check username and password towards database here
    } // End confirmUser
} // End class hk_Login

?>